package game;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
/*
 * Team: Sting Arrays
 * Creators: Devin La Fleur. and Jonathan Satchell.
 * Game: Defend Chernobly
 */
public class Upgrade extends JFrame {

	private JPanel contentPane;
	private final ButtonGroup buttonGroup = new ButtonGroup();

	JButton[] weaponButtons = new JButton[5];
	Gun[] weapons = {new Gun(Gun.Type.Berretta), new Gun(Gun.Type.Pump_Action), new Gun(Gun.Type.P90), new Gun(Gun.Type.Rifle), new Gun(Gun.Type.M16)};

	JButton[] fortifyButtons = new JButton[5];

	JLabel lblMoney;
	JLabel lblHealth;
	JLabel lblEquiped;

	Player player;

	int Round;

	public void avalible()
	{
		for(int x = 0; x<weapons.length;x++)
		{
			if(weapons[x].EQUIPED)
			{
				weaponButtons[x].setEnabled(true);
			}
			else
				weaponButtons[x].setEnabled(false);
		}
		for(int x = 0; x<fortifyButtons.length;x++)
		{
			if(!(fortifyButtons[x].isEnabled()))
			{

				break;
			}
			else
			{

			}
		}
	}

	public Upgrade(Player play, int numRounds) {
		Round = numRounds+1;
		player = play;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(new Rectangle(800,600));
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);

		weaponButtons[0] = new JButton("Berretta");
		//buttonGroup.add(weaponButtons[0]);
		weaponButtons[0].setBounds(350, 100, 150, 25);
		weaponButtons[0].addActionListener(new weapon_actionAdapter(this,weaponButtons[0]));
		contentPane.add(weaponButtons[0]);

		weaponButtons[1] = new JButton("Shot Gun - $20");
		//buttonGroup.add(weaponButtons[1]);
		weaponButtons[1].setBounds(350, 150, 150, 25);
		weaponButtons[1].addActionListener(new weapon_actionAdapter(this,weaponButtons[1]));
		contentPane.add(weaponButtons[1]);

		weaponButtons[2] = new JButton("P90 - $50");
		//buttonGroup.add(weaponButtons[2]);
		weaponButtons[2].setBounds(350, 200, 150, 25);
		weaponButtons[2].addActionListener(new weapon_actionAdapter(this,weaponButtons[2]));
		contentPane.add(weaponButtons[2]);

		weaponButtons[3] = new JButton("Sniper - $100");
		buttonGroup.add(weaponButtons[3]);
		weaponButtons[3].setBounds(350, 250, 150, 25);
		weaponButtons[3].addActionListener(new weapon_actionAdapter(this,weaponButtons[3]));
		contentPane.add(weaponButtons[3]);

		weaponButtons[4] = new JButton("M16 - $200");
		buttonGroup.add(weaponButtons[4]);
		weaponButtons[4].setBounds(350, 300, 150, 25);
		weaponButtons[4].addActionListener(new weapon_actionAdapter(this,weaponButtons[4]));
		contentPane.add(weaponButtons[4]);

		fortifyButtons[0] = new JButton("Straw - $50");

		
		fortifyButtons[0] = new JButton("Straw - $50");

		fortifyButtons[0].setBounds(30, 100, 89, 23);
		fortifyButtons[0].addActionListener(new fortify_actionAdapter(this,fortifyButtons[0]));
		contentPane.add(fortifyButtons[0]);


		fortifyButtons[1] = new JButton("Sticks - $75");

		fortifyButtons[1] = new JButton("Sticks - $75");

		fortifyButtons[1].setBounds(30, 150, 125, 23);
		fortifyButtons[1].addActionListener(new fortify_actionAdapter(this,fortifyButtons[1]));
		contentPane.add(fortifyButtons[1]);

		fortifyButtons[2] = new JButton("Bricks - $100");
		fortifyButtons[2].setBounds(30, 200, 125, 23);
		fortifyButtons[2].addActionListener(new fortify_actionAdapter(this,fortifyButtons[2]));
		contentPane.add(fortifyButtons[2]);

		fortifyButtons[3] = new JButton("Cement - $150");
		fortifyButtons[3].setBounds(30, 250, 125, 23);
		fortifyButtons[3].addActionListener(new fortify_actionAdapter(this,fortifyButtons[3]));
		contentPane.add(fortifyButtons[3]);

		fortifyButtons[4] = new JButton("Vibranium - $300");
		fortifyButtons[4].setBounds(30, 300, 150, 23);
		fortifyButtons[4].addActionListener(new fortify_actionAdapter(this,fortifyButtons[4]));
		contentPane.add(fortifyButtons[4]);

		JLabel lblFortify = new JLabel("Fortify your Defenses");
		lblFortify.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		lblFortify.setBounds(25, 30, 130, 19);
		contentPane.add(lblFortify);

		JLabel lblWeapons = new JLabel("Pick Your Weapon");
		lblWeapons.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		lblWeapons.setBounds(350, 34, 239, 23);
		contentPane.add(lblWeapons);

		JButton btnStart = new JButton("Next Day");
		btnStart.addActionListener(new Upgrade_btnStart_actionAdapter(this));
		btnStart.setBounds(350, 528, 89, 23);
		contentPane.add(btnStart);

		JButton btnExit = new JButton("Exit");
		btnExit.setBounds(520, 528, 89, 23);
		btnExit.addActionListener(new Upgrade_btnExit_actionAdapter(this));
		contentPane.add(btnExit);

		if(player.health < 100)
		{
			player.health = 100;
		}

		JLabel lblRound = new JLabel("Round: " + Round);
		lblRound.setBounds(648,125,89,14);
		contentPane.add(lblRound);

		lblMoney = new JLabel("Money: $"+player.money+"");
		lblMoney.setBounds(648, 34, 89, 14);
		contentPane.add(lblMoney);

		lblHealth = new JLabel("Health: "+player.health+"");
		lblHealth.setBounds(648, 70, 89, 14);
		contentPane.add(lblHealth);

		lblEquiped = new JLabel("Equiped: ");
		lblEquiped.setBounds(648, 95, 126, 14);
		lblEquiped.setText("Equiped: " + player.gun.toString);
		contentPane.add(lblEquiped);

		JLabel lblgrayedSelected = new JLabel("(Grayed = Selected)");
		lblgrayedSelected.setBounds(360, 68, 113, 14);
		contentPane.add(lblgrayedSelected);
		
		player.addGun(weapons[0]);
		for(int x = 0; x < weaponButtons.length;x++)
		{
			//sets the weapon icons and checks if the player has already unlocked a gun and which gun is equipped from last round
			ImageIcon img = new ImageIcon(weapons[x].img.getImage( ).getScaledInstance(weapons[x].WIDTH+40,weapons[x].HEIGHT+20, Image.SCALE_SMOOTH));
			weaponButtons[x].setIcon(img);
			if(!(player.money >= weapons[x].cost) || player.guns.contains(weapons[x]))
			{
				weaponButtons[x].setEnabled(false);
				if(weapons[x].equals(player.gun))
				{
					lblEquiped.setText("Equiped: " + player.gun.toString);
				}
			}
		}

	}

	public void btnExit_actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		System.exit(1);
	}
	//Starts next round
	public void btnStart_actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		this.setVisible(false);
		GameFrame game = new GameFrame(player, Round);

        // Center the window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = game.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        game.setLocation((screenSize.width - frameSize.width) / 2,
                          (screenSize.height - frameSize.height) / 2);
        game.setVisible(true);
	}


	public void weapons_actionPerformed(ActionEvent e, JButton button) {
		// TODO Auto-generated method stub

		//loops through the weaponButtons
		for(int x = 0; x < weaponButtons.length;x++)
		{
			//checks if its the button pressed, and if the player has unlocked the gun already or has enough to buy it
			if(button == weaponButtons[x] && (player.guns.contains(weapons[x]) || player.money >= weapons[x].cost))
			{
				//buys the gun if the player doesn't already have it
				if(!player.guns.contains(weapons[x]))
				{
					player.getPaid(-weapons[x].cost);
					player.guns.add(weapons[x]);
				}
				button.setEnabled(false);
				player.setGun(weapons[x]);//sets the gun to the currently equipped gun

				lblEquiped.setText("Equiped: " + player.gun.toString);
			}
			//otherwise it doesn't do anything
			else
			{
				weaponButtons[x].setEnabled(true);
			}
			lblMoney.setText("Money: $"+player.money);
		}
	}

	public void fortify_actionPerformed(ActionEvent e, JButton button) {
		// TODO Auto-generated method stub

		//loops through fortifyButtons
		for(int x = 0; x < fortifyButtons.length;x++)
		{
			//Checks which type the button stands for and if the player can buy it
			if(button == fortifyButtons[x] && player.money >= player.getCostArmor(button.getText().substring(0,3)))
			{
				button.setEnabled(true);
				if(button.getText().substring(0,3).equals("Sti"))
				{
					player.addArmor(Player.Armor.Sticks);
				}
				else if(button.getText().substring(0,3).equalsIgnoreCase("Str"))
				{
					player.addArmor(Player.Armor.Straw);
				}
				else if(button.getText().substring(0,3).equalsIgnoreCase("Bri"))
				{
					player.addArmor(Player.Armor.Bricks);
				}
				else if(button.getText().substring(0,3).equalsIgnoreCase("Cem"))
				{
					player.addArmor(Player.Armor.Cement);
				}
				else if(button.getText().substring(0,3).equalsIgnoreCase("Vib"))
				{
					player.addArmor(Player.Armor.Vibranium);
				}
				player.getPaid(-player.getCostArmor(button.getText().substring(0,3)));
			}
			//otherwise it doesn;t do anything
			else
			{
				fortifyButtons[x].setEnabled(true);
			}

		}
		lblHealth.setText("Health: "+player.health+"");
		lblMoney.setText("Money: $"+player.money);
	}
}
class weapon_actionAdapter implements ActionListener{
	private Upgrade adaptee;
	private JButton button;
    weapon_actionAdapter(Upgrade adaptee, JButton button) {
        this.adaptee = adaptee;
        this.button = button;
    }
	public void actionPerformed(ActionEvent e) {
		adaptee.weapons_actionPerformed(e, button);
	}

}
class Upgrade_btnExit_actionAdapter implements ActionListener {
	private Upgrade adaptee;
    Upgrade_btnExit_actionAdapter(Upgrade adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.btnExit_actionPerformed(e);
    }

}
class Upgrade_btnStart_actionAdapter implements ActionListener {
	private Upgrade adaptee;
    Upgrade_btnStart_actionAdapter(Upgrade adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.btnStart_actionPerformed(e);
    }

}
class fortify_actionAdapter implements ActionListener{
	private Upgrade adaptee;
	private JButton button;
    fortify_actionAdapter(Upgrade adaptee, JButton button) {
        this.adaptee = adaptee;
        this.button = button;
    }
	public void actionPerformed(ActionEvent e) {
		adaptee.fortify_actionPerformed(e, button);
	}

}